package com.qf.business.course.controller;

import com.qf.business.course.service.CourseService;
import com.qf.business.course.service.UserBuyRecoredService;
import com.qf.commons.base.result.R;
import com.qf.commons.base.result.Rs;
import com.qf.commons.base.user.BaseUser;
import com.qf.commons.web.aspect.annotation.GetUser;
import com.qf.commons.web.aspect.utils.UserUtils;
import com.qf.data.course.entity.Course;
import com.qf.data.course.entity.UserBuyRecored;
import com.qf.data.user.entity.Student;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * author Ken
 * create_time 2022/11/29
 */
@RestController
@RequestMapping("/course")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private UserBuyRecoredService userBuyRecoredService;

    @RequestMapping("/insert")
    public R insert(Course course){
        courseService.save(course);
        return Rs.create();
    }

    /**
     * 查询课程
     * @return
     */
    @RequestMapping("/list")
    public R query(){
        List<Course> course = courseService.list();
        return Rs.create(course);
    }

    /**
     * 根据课程id 查询课程信息
     * @param cid
     * @return
     */
    @RequestMapping("/queryCourseById")
    public R queryCourseById(Integer cid){
        Course course = courseService.queryCourseById(cid);
        log.debug("[query course] 根据课程id查询课程详情 - {}", course);
        return Rs.create(course);
    }

    /**
     * 根据课程id 购买课程信息
     * @param cid
     * @return
     */
    @GetUser
    @RequestMapping("/buy")
    public R buyCourse(Integer cid) {
        BaseUser user = UserUtils.getUser(Student.class);
        Long id = user.getId();
        log.debug("[buy course] 购买课程 - {} - {}", id, cid);

        UserBuyRecored userBuyRecored = new UserBuyRecored()
                .setUid(id.intValue())
                .setCid(cid);
        userBuyRecoredService.save(userBuyRecored);
        return Rs.create();
    }

    /**
     * 查询我的课程列表
     * @return
     */
    @GetUser
    @RequestMapping("/queryMyCourse")
    public R queryMyCourse(){
        BaseUser user = UserUtils.getUser(Student.class);
        Long uid = user.getId();

        //查询当前用户的购买记录列表
        List<UserBuyRecored> recoreds = userBuyRecoredService.query().select("cid").eq("uid", uid).list();
        if (Collections.isEmpty(recoreds)) {
            return Rs.create();
        }

        List<Course> courseInfos = courseService.query()
                .in("id", recoreds.stream().map(userBuyRecored -> userBuyRecored.getCid()).collect(Collectors.toList()))
                .orderByDesc("status")
                .orderByAsc("begin_date")
                .list();
        return Rs.create(courseInfos);
    }

    /**
     * 根据课程id，修改课程的状态
     * @param cid
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public R updateCourseStatus(Long cid, Integer status) {
        Course course = courseService.getById(cid);
        course.setStatus(status);
        courseService.updateById(course);
        return Rs.create();
    }
}
